package com.timmy.chi.utils.network;

import android.util.Log;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.timmy.chi.MyApp;
import com.timmy.chi.utils.Constants;

import java.io.File;

import okhttp3.Call;
import okhttp3.Response;

import static android.content.ContentValues.TAG;

/**
 * 网络请求框架
 */

public class MyHttp {

    /**
     * 登录
     */
    public static void login(String userName, String password, final ResultCallback callback) {
        OkGo.post(Constants.USER)
                .params("method", "login")
                .params("userName", userName)
                .params("password", password)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }

    /**
     * 查询所有菜品
     */
    public static void queryAllFood(final ResultCallback callback) {
        OkGo.get(Constants.FOOD)
                .params("method", "findAll")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }

    /**
     * 查询指定菜品
     */
    public static void queryAllFood(int id, final ResultCallback callback) {
        OkGo.post(Constants.FOOD)
                .params("method", "findById")
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }


    /**
     * 添加菜品
     */
    public static void addFood(
            String foodName,
            double price,
            String material,
            String image,
            int cookingtime,
            int sid,
            final ResultCallback callback) {

        OkGo.get(Constants.FOOD)
                .params("method", "add")
                .params("name", foodName)
                .params("price", price)
                .params("material", material)
                .params("image", image)
                .params("cookingtime", cookingtime)
                .params("sid", sid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }

    /**
     * 删除菜品
     */
    public static void deleteFood(int id, final ResultCallback callback) {
        OkGo.post(Constants.FOOD)
                .params("method", "deleteById")
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }

    //--------------------订单-----------------------------

    /**
     * 添加订单
     */
    public static void addOrder(String orderID, final ResultCallback callback) {
        OkGo.post(Constants.ORDER)
                .params("method", "add")
                .params("food", orderID)
                .params("uid", 1)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }

    /**
     * 通过订单id, 查询该订单下所有的菜品
     */
    public static void getFood(int orderID, final ResultCallback callback) {
        OkGo.get(Constants.ORDER)
                .params("method", "findById")
                .params("id", orderID)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }

    /**
     * 通过用户id 查询该用户下所有的订单
     */
    public static void getOrder(final ResultCallback callback) {
        OkGo.get(Constants.ORDER)
                .params("method", "findByUid")
                .params("uid", MyApp.uId)           // 用户id
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }


    /**
     * 通过用户id 查询该用户下所有的订单
     */
    public static void getHouTaiData(final ResultCallback callback) {
        OkGo.get(Constants.ORDER)
                .params("method", "findByUid")
                .params("uid", -1)           // 用户id
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }

    /**
     * 修改某一订单中某一菜品的烹饪状态
     * 厨师专用
     */
    public static void updateState(int foodID, final ResultCallback callback) {
        OkGo.get(Constants.ORDER)
                .params("method", "updateState")
                .params("id", foodID)           // 用户id
                .params("sid", 2)           // 烹饪状态
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }

    /**
     * 修改某一订单中某一菜品的烹饪状态
     * 厨师专用
     */
    public static void deleteState(int foodID, final ResultCallback callback) {
        OkGo.get(Constants.ORDER)
                .params("method", "updateState")
                .params("id", foodID)           // 用户id
                .params("sid", 3)           // 烹饪状态
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }

    /**
     * 下架某一菜品
     * 管理员专用
     */
    public static void closeFood(int foodID, final ResultCallback callback) {
        OkGo.get(Constants.FOOD)
                .params("method", "closeById")
                .params("id", foodID)           //food id
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        callback.onSuccess(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }
                });
    }

    public static void upLoadingImg(File file) {
        OkGo.post("http://120.25.226.120:8080/resource/uploadImage")
                .tag(MyApp.getInstance())
                .params("file", file)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.i(TAG, "上传图片成功: " + s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {

                        Log.i(TAG, "上传图片失败");
                        super.onError(call, response, e);
                    }
                });
    }

}
